package com.bignerdranch.android.ecar.Fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.ecar.Activity.FaultActivity;
import com.bignerdranch.android.ecar.Activity.MaintainActivity;
import com.bignerdranch.android.ecar.Activity.NewsListActivity;
import com.bignerdranch.android.ecar.Activity.RefuelActivity;
import com.bignerdranch.android.ecar.Activity.RoadActivity;
import com.bignerdranch.android.ecar.Activity.UsedCarActivity;
import com.bignerdranch.android.ecar.Activity.WashActivity;
import com.bignerdranch.android.ecar.Adapter.HomeAdapter;
import com.bignerdranch.android.ecar.Bean.HomeBean;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.CheckNet;
import com.bignerdranch.android.ecar.Utils.MyImageCache;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/18.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {

    private ListView part_list;
    private List<HomeBean> mData;
    private LinearLayout fault;
    private LinearLayout refuel;
    private LinearLayout wash;
    private LinearLayout road;
    private RelativeLayout used_car;
    private RelativeLayout maintain;
    private NetworkImageView weather_img;
    private TextView weather;
    private TextView temperature;
    private TextView wash_car;
    private TextView update;
    private TextView city;
    private TextView more;

    private String url = "http://route.showapi.com/170-47?showapi_sign=b697ebeae51b4525b7ea856bade2d325&showapi_appid=20076&areaName=河南";
    private LinearLayout progress;
    private LinearLayout wifi;
    private Button wifi_but;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        initView(view);
        initWeather();
        if (CheckNet.getNet(getActivity())){
            initData();
//            Log.e("----->>>>","CheckNet"+CheckNet.getNet(getActivity()));
        }else {
            wifi.setVisibility(View.VISIBLE);
//            Log.e("-----else>>>>","CheckNet"+CheckNet.getNet(getActivity()));
        }
        return view;
    }

    private void initView(View view) {

        more = (TextView)view.findViewById(R.id.more);
        more.setOnClickListener(this);
        part_list = (ListView) view.findViewById(R.id.part_list);
        fault = (LinearLayout) view.findViewById(R.id.fault);
        fault.setOnClickListener(this);
        refuel = (LinearLayout) view.findViewById(R.id.refuel);
        refuel.setOnClickListener(this);
        wash = (LinearLayout) view.findViewById(R.id.wash);
        wash.setOnClickListener(this);
        road = (LinearLayout) view.findViewById(R.id.road);
        road.setOnClickListener(this);
        used_car = (RelativeLayout) view.findViewById(R.id.used_car);
        used_car.setOnClickListener(this);
        maintain = (RelativeLayout) view.findViewById(R.id.maintain);
        maintain.setOnClickListener(this);
        weather_img = (NetworkImageView) view.findViewById(R.id.weather_img);
        weather_img.setOnClickListener(this);
        weather = (TextView) view.findViewById(R.id.weather);
        weather.setOnClickListener(this);
        temperature = (TextView) view.findViewById(R.id.temperature);
        temperature.setOnClickListener(this);
        wash_car = (TextView) view.findViewById(R.id.wash_car);
        wash_car.setOnClickListener(this);
        update = (TextView) view.findViewById(R.id.update);
        update.setOnClickListener(this);
        city = (TextView) view.findViewById(R.id.city);
        progress = (LinearLayout) view.findViewById(R.id.progress);
        wifi = (LinearLayout) view.findViewById(R.id.wifi);
        wifi_but = (Button)view.findViewById(R.id.wifi_but);
        wifi_but.setOnClickListener(this);
    }

    private void initData() {
        mData = new ArrayList<>();
        RequestUtil.executeJSONObject(getContext(), url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
//                    Log.e("=====initData==", "onResponse");
                    progress.setVisibility(View.INVISIBLE);
                    part_list.setVisibility(View.VISIBLE);
                    JSONObject object = o.getJSONObject("showapi_res_body").getJSONObject("pagebean");
                    JSONArray array = object.getJSONArray("contentlist");
//                    Log.e("=====homefragment==", array.length() + "");
                    for(int i = 0;i < array.length() ;i++){
                        HomeBean bean = new HomeBean(array.getJSONObject(i).getString("title"),
                                array.getJSONObject(i).getString("link"));
                        mData.add(bean);
                    }
                    part_list.setAdapter(new HomeAdapter(getActivity(), mData));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        part_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(getActivity(),"条目点击"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(mData.get(position).getLink()));
                startActivity(intent);

            }
        });

      /*  for (int i = 0; i < 50; i++) {
            HomeBean bean = new HomeBean("新闻" + i);
            mData.add(bean);
        }*/
//        Log.e("=======", mData.size() + "");
    }

    private void initWeather(){
        RequestUtil.executeJSONObject(getActivity(), URLBank.weather_url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    JSONObject object = o.getJSONObject("showapi_res_body");
                    String update_text = object.getString("time");
                    String hh = update_text.substring(8,10);
                    String mm = update_text.substring(10,12);
                    String city_text = object.getJSONObject("cityInfo").getString("c3");
                    String weather_img_text = object.getJSONObject("f1").getString("day_weather_pic");
                    String weather_text = object.getJSONObject("f1").getString("day_weather");
                    String temperature_text =
                            object.getJSONObject("f1").getString("night_air_temperature")+"° ~  "
                                    +object.getJSONObject("f1").getString("day_air_temperature")+"°";

                    String wash_text = object.getJSONObject("f1").getJSONObject("index").getJSONObject("wash_car").getString("title");
                    ImageLoader loader = new ImageLoader(RequestUtil.getRequestQueue(getActivity()), new MyImageCache());
                    weather_img.setDefaultImageResId(R.drawable.sample);
                    weather_img.setErrorImageResId(R.drawable.sample);
                    weather_img.setImageUrl(weather_img_text, loader);

                    city.setText(city_text);
                    weather.setText(weather_text);
                    temperature.setText(temperature_text);
                    wash_car.setText("洗车指数："+wash_text);
                    update.setText(hh+":"+mm+" 更新");

                  /*  Log.e("======",weather_img_text);
                    Log.e("======",weather_text);
                    Log.e("======",temperature_text);
                    Log.e("======",wash_text);
                    Log.e("======",hh+":"+mm);
                    Log.e("======",city_text);*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fault:
                Intent intent = new Intent(getActivity(), FaultActivity.class);
                startActivity(intent);
                break;
            case R.id.refuel:
                Intent intent1 = new Intent(getActivity(), RefuelActivity.class);
                startActivity(intent1);
                break;
            case R.id.wash:
                Intent intent2 = new Intent(getActivity(), WashActivity.class);
                startActivity(intent2);
                break;
            case R.id.road:
                Intent intent3 = new Intent(getActivity(), RoadActivity.class);
                startActivity(intent3);
                break;
            case R.id.used_car:
                Intent intent4 = new Intent(getActivity(), UsedCarActivity.class);
                startActivity(intent4);
                break;
            case R.id.maintain:
                Intent intent5 = new Intent(getActivity(), MaintainActivity.class);
                startActivity(intent5);
                break;
            case R.id.more:
                Intent intent6 = new Intent(getActivity(), NewsListActivity.class);
                startActivity(intent6);
//                getActivity().finish();
                break;
            case R.id.wifi_but:
                if (CheckNet.getNet(getActivity())){
                    initData();
                    wifi.setVisibility(View.INVISIBLE);
//                    Log.e("----->>>>","CheckNet"+CheckNet.getNet(getActivity()));
                }else {
                    wifi.setVisibility(View.VISIBLE);
//                    Log.e("-----else>>>>","CheckNet"+CheckNet.getNet(getActivity()));
                }
                break;
        }

    }
}
